
var Users = new Class({
    Implements: Options,
    options: {
        tagid: null
    },
    initialize: function(options) {
        this.setOptions(options);
        this.panel = document.id(this.options.tagid);
        if(!this.panel) return;

        this.panel.empty(); // clean panel

        this.tabs = new Tabs();

        this.createTableHead();

        new Request({
            url: '/Admin/webstat/getFromMySQL',
            method: 'get',
            onRequest: function() {
                new ShowBox().say('Loading...');
                new Loader().show();
            },
            onComplete: function(resp) {
                var data;
                try {
                    data = eval('[' + resp + ']');
                }
                catch(err){alert('Error...' + err);return;}
                new Loader().hide();

                this.fillContent(data);
            }.bind(this)
        }).send('table=login&ex=getUsers&nc=' + Date.now());

    },
    createTableHead: function() {
        this.content = new Element('div#content');
        this.listData = new Element('table#listData', {'rules':'groups'});

        this.panel.adopt(
            new Element('div.tl[html=<div class="tr"><div>&nbsp;</div></div>]'),
            this.content.adopt(this.listData),
            new Element('div.bl[html=<div class="br"><div>&nbsp;</div></div>]')
        )

        var tagtr = new Element('tr').adopt(
            new Element('th.brdCol.w15[text=User]'),
            new Element('th.brdCol.w15[text=Password]'),
            new Element('th.brdCol.w15[text=AccwareCatID]'),
            new Element('th.brdCol.w15[text=Active]')
        );
        var idx = 4;
        this.tabs.getTabName().each(function(name) {
            idx++;
            tagtr.adopt(new Element('th.brdCol.w15[text=' + name + ']'));
        });
        idx++;
        tagtr.adopt(
            new Element('th.brdCol.w15')
        );
        this.listData.adopt(
            new Element('thead').adopt(tagtr),
            new Element('tfoot').adopt(
                new Element('tr').adopt(
                    new Element('th.tdFull[html=&nbsp;][colspan=' + idx + ']')
                )
            ),
            new Element('tbody')
        );
    },
    fillContent: function(users) {

        var tbody = this.listData.getElement('tbody');
        tbody.empty();

        var tagtr,color=false;
        var tabsMask = this.tabs.getTabMask();
        users.each(function(user) {

            tagtr = new Element('tr#user' + user.id + '[style=background-color:#f' + ((color = !color) ? 'fffff' : 'cffcd') + ';]',{
                'events': {
                    'mouseenter': function() {this.addClass('rowHover');},
                    'mouseleave': function() {this.removeClass('rowHover');}
                }
            }).adopt(
                new Element('td.brdCol.center.w50[text=' + user.name +  ']'),
                new Element('td.brdCol.center.w80').adopt(
                    new Element('input[type=text][style=width:80px][maxlength=15][value=' + user.passwd + ']')
                ),
                new Element('td.brdCol.center.w15[text=' + user.catid + ']'),
                new Element('td.imgCol').adopt(
                    new Element('a.check' + (user.act ? '1' : '0') + '[html=&nbsp;]',{
                        'events': {
                            'click': this.changeRadio.bind(this),
                            'focus': function() {this.blur();}
                        }
                    }).store('bitactive', true)
                )
            );
            tabsMask.each(function(mask) {
                tagtr.adopt(
                    new Element('td.imgCol').adopt(
                        new Element('a.check' + (user.mask & mask ? '1' : '0') + '[html=&nbsp;]',{
                            'events': {
                                'click': this.changeRadio.bind(this),
                                'focus': function() {this.blur();}
                            }
                        }).store('bitmask', mask)
                    )
                );
            },this);
            // save
            tagtr.adopt(
                new Element('td.brdCol.center.w15').adopt(
                    new Element('div#selDate').adopt(
                        new Element('input[type=submit][value=Save]',{
                            'events': {
                                'click': this.saveMask.bind(this),
                                'focus': function() {this.blur();}
                            }
                        }).store('userid', user.id)
                    )
                )
            );

            tbody.adopt(tagtr);

        }, this);
        this.listData.adopt(tbody);

    },
    changeRadio: function(ev) {
        var active = ev.target.get('class') == 'check0' ? true : false;
        ev.target.set('class', 'check' + (active ? '1' : '0'));
    },
    saveMask: function(ev) {
        var userid = ev.target.retrieve('userid');
        var elms = document.id('user' + userid).getElements('a[class^="check"]');
        var mask = 0,active,bitmask,useractive = false;
        elms.each(function(elm) {
            bitmask = elm.retrieve('bitmask');
            active = elm.get('class') == 'check1' ? 1 : 0;
            if(bitmask) { // tab bottom
                mask |= (active ? bitmask : 0);
            }
            if(elm.retrieve('bitactive')) { // active bottom
                useractive = active;
            }
        });

        var passwd = document.id('user' + userid).getElements('input[type="text"]').get('value');

        new Request({
            url: '/Admin/webstat/getFromMySQL',
            method: 'get',
            onRequest: function() {
                new ShowBox().say('Update...');
                new Loader().show();
            },
            onComplete: function() {
                new Loader().hide();
            }
        }).send('table=login&ex=updateRootUser&id=' + userid  + '&passwd=' + passwd + '&mask=' + mask + '&act=' + useractive + '&nc=' + Date.now());

    }
});
